module.exports = {
    state: {
        article: {},
        detail: {},
        remarks: {}
    },
    mutations: {
        articleList (state, {id, data}) {
            state.article = {
                channel: id,
                list: data
            }
        },
        articleDetail (state, {id, data}) {
            state.detail = {
                article: id,
                content: data
            }
        },
        articleRates (state, {id, data}) {
            state.detail.content.user_rates = data
            state.detail.content.art_rates = data.length
            state.detail.content.art_rated = 1
        },
        articleRemarks (state, {id, data}) {
            if(state.detail.article == id) {
                state.remarks = {
                    article: id,
                    list: data
                }
            }
        },
        activityUsers (state, {id, data}) {
            if(state.detail.article == id) {
                state.detail.content.users_join = data
            }
        },
        voteUsers (state, {id, data}) {
            if(state.detail.article == id) {
                state.detail.content.users_vote = data
            }
        }
    },
    actions: {
        getArticle ({commit, state}, id) {
            $store.api.call('article_list', {
                message: '获取文章清单失败',
                params: {
                    channel: id
                }
            }, ({data}) => {
                commit('articleList', {id, data})
            })
        },
        readArticle ({commit, state}, {channel, id, resolve}) {
            $store.api.call('article_detail', {
                message: '读取文章内容失败',
                params: {
                    channel,
                    article: id
                }
            }, ({data}) => {
                commit('articleDetail', {id, data})
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        rateArticle ({commit, state}, {channel, id, resolve}) {
            $store.api.call('article_rate', {
                message: '评价文章失败',
                params: {
                    channel,
                    article: id
                }
            }, ({data}) => {
                commit('articleRates', {id, data})
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        getRemarks ({commit, state}, {channel, id, resolve}) {
            $store.api.call('article_remarks', {
                message: '加载评论失败',
                params: {
                    channel,
                    article: id
                }
            }, ({data}) => {
                commit('articleRemarks', {id, data})
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        remarkArticle ({commit, state}, {channel, id, text}) {
            const art_id = state.detail.article
            $store.api.call('article_remark', {
                message: '发送评论失败',
                params: {
                    channel,
                    article: art_id
                },
                form: {
                    remark: text
                }
            }, ({data}) => {
                commit('articleRemarks', {id: art_id, data})
            })
        },
        joinActivity ({commit, state}, {id, resolve}) {
            const art_id = state.detail.article
            $store.api.call('activity_join', {
                message: '活动报名失败',
                params: {
                    article: art_id
                }
            }, ({data}) => {
                commit('activityUsers', {id, art_id, data})
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        cancelActivity ({commit, state}, {id, resolve}) {
            const art_id = state.detail.article
            $store.api.call('activity_cancel', {
                message: '活动取消报名失败',
                params: {
                    article: art_id
                }
            }, ({data}) => {
                commit('activityUsers', {id, art_id, data})
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        voteArticle ({commit, state}, {id, vote, resolve}) {
            const art_id = state.detail.article
            $store.api.call('article_vote', {
                message: '投票失败',
                params: {
                    article: art_id
                },
                form: {
                    vote
                }
            }, ({data}) => {
                commit('voteUsers', {id, art_id, data})
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        surveySubmit ({commit, state}, {id, survey, resolve}) {
            const art_id = state.detail.article
            $store.api.call('survey_submit', {
                message: '提交问卷失败',
                params: {
                    article: art_id
                },
                form: survey
            }, ({data}) => {
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        }
    },
    getters: {
        articleList (state) {
            return state.article
        },
        articleContent (state) {
            return state.detail
        },
        articleRemarks (state) {
            return state.remarks
        }
    }
}